import struct

from ..base import BaseParser


class GetCompanyInfoContent(BaseParser):

    def setParams(self, market, code, filename, start, length):
        code = code.encode('utf-8') if type(code) is str else code

        filename = filename.encode('utf-8') if type(filename) is str else filename
        filename = filename.ljust(80, b'\x00') if len(filename) != 80 else filename

        pkg = bytearray.fromhex(u'0c 07 10 9c 00 01 68 00 68 00 d0 02')
        pkg.extend(struct.pack(u'<H6sH80sIII', market, code, 0, filename, start, length, 0))

        self.send_pkg = pkg

    def parseResponse(self, body_buf):
        pos = 12

        _, length = struct.unpack(u'<10sH', body_buf[:pos])
        return body_buf[pos: pos + length].decode('gbk')
